{% extends 'project_python/layout_pycharm.html' %}
{% load static %}

{% block panel_js_files %}
<script src="{% static 'js/sharedkit.js' %}"></script>
{% endblock %}

{% block html_js_code %}
{{ block.super }}
<script>
layui.use(function(){
    var $ = layui.$;
    var layer = layui.layer;
    var util = layui.util;

    util.on('lay-on', {
        // import: function(){
        //     var project_path = $(this).data('projectpath');
        //     var project_name = $(this).data('projectname');
        //     var sdk_path = $(this).data('sdkpath');
        //     var sdk_name = $(this).data('sdkname');
        //     const csrftoken = getCookie('csrftoken');
        //     $.ajax({
        //         url: "",
        //         method: 'POST',
        //         headers: {'X-CSRFToken': csrftoken},
        //         mode: 'same-origin',
        //         data:{
        //             "project_path": project_path,
        //             "project_name": project_name,
        //             "sdk_name": sdk_name,
        //             "sdk_path": sdk_path,
        //         },
        //         success: function(response){
        //             location.reload();
        //         },
        //             error: function(xhr, status, message) {
        //                 let errorMsg = '操作失败: ';
        //                 if (xhr.responseJSON && xhr.responseJSON.message) {
        //                     errorMsg += xhr.responseJSON.message;
        //                 } else {
        //                     errorMsg += message;
        //                 }
        //                 layer.alert(errorMsg, {icon: 2, title:null});
        //             }
        //     })
        // },
        projectDel: function(){
          const projectPath = $(this).data('path');
          const projectName = $(this).data('projectname');
          layer.confirm('确定要删除 Pycharm 里的项目吗？', {icon:3, title:null}, function(){
              $.ajax({
                  url: "",
                  type: 'POST',
                  headers: {'X-CSRFToken': getCookie('csrftoken')},
                  mode:'same-origin',
                  data: {
                      "project_path": projectPath,
                      "project_name": projectName,
                  },
                  success: function(response){
                      layer.msg('删除完成');
                      location.href = "{% url 'project_python:pycharm_project' %}";
                  },
                  error:function(xhr, status, message){
                      let errorMsg = '操作失败: ';
                      if (xhr.responseJSON && xhr.responseJSON.message) {
                          errorMsg += xhr.responseJSON.message;
                      } else {
                          errorMsg += message;
                      }
                      layer.alert(errorMsg, {icon: 2, title:null}); 
                  }
              })
          }, function(){layer.close();})            
        },
        pycharmopenproject: function(){
          const csrftoken = getCookie('csrftoken');
            var loadIndex = layer.msg('使用 Pycharm 打开项目中...', {icon: 16,shade: 0.6, title:null});
            $.ajax({
                url: "{% url 'project_python:pycharm_project_open' %}",
                type: "POST",
                headers: {'X-CSRFToken': csrftoken},
                mode: 'same-origin',
                data:{
                  'path':$(this).data('path'),
                },
                success:function(response){
                    loadIndex.close();
                },
                error: function(xhr, status, message){
                    let errorMsg = '操作失败: ';
                    if (xhr.responseJSON && xhr.responseJSON.message) {
                        errorMsg += xhr.responseJSON.message;
                    } else {
                        errorMsg += message;
                    }
                    layer.alert(errorMsg, {icon: 2, title:null});
                }
            })
        }
    })
})
</script>
{% endblock %}

{% block pycharm_content %}

{#<div class="row">#}
{#    <div class="col-12 col-md-12 col-lg-8 order-2 order-md-1">#}

{% if not pycharm_projects %}
没有找到 Pycharm 项目信息~~
{% else %}
{% for autoid, info in pycharm_projects.items %}

    <table class="table table-hover">
      <tbody>
          <tr>
            <td class="mailbox-subject" style="border-top:none; border-bottom: 1px solid #dee2c6;">
                <div class="text-bold">{{ info.project_name }}</div>
                <div class="text-muted">项目位置：{{ info.project_path }}</div>
                <div class="text-muted">环境名称：{{ info.sdk_name }}</div>
                <div class="text-muted">Python版本：{{ info.sdk_version }}</div>
                <div class="text-muted">解释器路径：{{ info.sdk_path }}</div>
            </td>
            <td style="width:120px;border-top:none; border-bottom: 1px solid #dee2c6; padding:5px;">
{% if pycharm %}
    <button type="button" data-path="{{ info.project_path }}" class="btn btn-info btn-xs btn-block" lay-on="pycharmopenproject">
      在Pycharm打开
    </button>
{% endif %}
<button type="button" data-path="{{ info.project_path }}" data-projectname="{{ info.project_name }}"  class="btn btn-outline-danger btn-xs mt-1 btn-block" lay-on="projectDel">删除</a>
            </td>
          </tr>
      </tbody>
    </table>

    {% endfor %}

{% endif %}


{% endblock %}